package com.qian2.pricesharingapplication;

import java.io.File;
import java.util.ArrayList;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.Bitmap.CompressFormat;
import android.location.Location;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.pricesharingapplication.R;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.UsingFreqLimitedMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.nostra13.universalimageloader.utils.StorageUtils;

public class MainArrayAdapter extends ArrayAdapter<String> {

	private final Context context;
	private ArrayList<String> data_array;
	public DisplayImageOptions options;
	public ImageLoader imageLoader = ImageLoader.getInstance();
	int fail_to_get_image=R.drawable.ic_action_picture;

	public MainArrayAdapter(Context context, ArrayList<String> list_of_ids,int error_image) {
		super(context, R.layout.activity_list_item, list_of_ids);
		this.context = context;
		this.data_array = list_of_ids;
		this.fail_to_get_image=error_image;
		
		 File cacheDir = StorageUtils.getCacheDirectory(context);//for caching

		 // Get singletone instance of ImageLoader
		 imageLoader = ImageLoader.getInstance();
		 // Create configuration for ImageLoader (all options are optional)
		 ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
		 // You can pass your own memory cache implementation
		.discCache(new UnlimitedDiscCache(cacheDir)) // You can pass your own disc cache implementation
		.discCacheFileNameGenerator(new HashCodeFileNameGenerator())
		.enableLogging()
		.build();
		// Initialize ImageLoader with created configuration. Do it once.
		imageLoader.init(config);
		options = new DisplayImageOptions.Builder()
		.showStubImage(fail_to_get_image)//display stub image untik image is loaded
		.cacheInMemory()
		.cacheOnDisc()
		.build();
	}
	
	public MainArrayAdapter(Context context, ArrayList<String> list_of_ids) {

		super(context, R.layout.activity_list_item, list_of_ids);
		this.context = context;
		this.data_array = list_of_ids;

		 File cacheDir = StorageUtils.getOwnCacheDirectory(context, "your_folder");//for caching

		 // Get singletone instance of ImageLoader
		 imageLoader = ImageLoader.getInstance();
		 // Create configuration for ImageLoader (all options are optional)
		 ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
		 // You can pass your own memory cache implementation
		.discCache(new UnlimitedDiscCache(cacheDir)) // You can pass your own disc cache implementation
		.discCacheFileNameGenerator(new HashCodeFileNameGenerator())
		.enableLogging()
		.build();
		// Initialize ImageLoader with created configuration. Do it once.
		imageLoader.init(config);
		options = new DisplayImageOptions.Builder()
		.showStubImage(fail_to_get_image)//display stub image untik image is loaded
		.cacheInMemory()
		.cacheOnDisc()
		.build();

	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View rowView = inflater.inflate(R.layout.activity_list_item, parent, false);

		
		TextView tv_uid = (TextView) rowView.findViewById(R.id.uid);
		TextView tv_brand = (TextView) rowView.findViewById(R.id.brand);
		TextView tv_name = (TextView) rowView.findViewById(R.id.name);

		ImageView imageView_product_icon = (ImageView) rowView.findViewById(R.id.productImage);
	
		ImageLoadingListener mImageLoadingListenr = new ImageLoadingListener() {

			@Override
			public void onLoadingStarted(String arg0, View arg1) {
				// Log.e("* started *", String.valueOf("complete"));
				((ImageView) arg1).setImageResource(R.drawable.ic_action_picture);
			}

			@Override
			public void onLoadingFailed(String arg0, View arg1, FailReason arg2) {
				// Log.e("* failed !*", String.valueOf("complete"));
				((ImageView) arg1).setImageResource(R.drawable.ic_action_picture);
			}

			@Override
			public void onLoadingComplete(String arg0, View arg1, Bitmap arg2) {
				// Log.e("* complete *", String.valueOf("complete"));
			}

			@Override
			public void onLoadingCancelled(String arg0, View arg1) {

			}
		};

		try {

			String[] aux = data_array.get(position).split(" ### ");

			String id = aux[0];			
			String uid = aux[1];
			String  brand= aux[2];
			String name = aux[3];
			String image = "";
			if (aux.length > 4) image = aux[4];

			// ------- image ---------
			try {
				if (image.length() > 4) imageLoader.displayImage(image, imageView_product_icon, options, mImageLoadingListenr);				
			} catch (Exception ex) {
			}

			

			tv_uid.setText(uid);
			tv_brand.setText(brand);
			tv_name.setText(name);
		} catch (Exception e) {
			// TODO: handle exception
		}

		return rowView;
	}

}